/*
 * LoggerFactory.cpp
 *
 *  Created on: Jan 11, 2012
 *      Author: sihai
 */

#include "log4cpp.h"
#include "consolelogger.h"
#include "fixedsizerollingfilelogger.h"
#include "dailyrollingfilelogger.h"

using namespace std;
Logger* LoggerFactory::get_logger(LoggerType type, int buffersize, ...)
{
	assert(type >= CONSOLE_LOGGER && type <= DAILY_ROLLING_FILE_LOGGER);
	assert(buffersize >= 0);
	if(type == FIXED_SIZE_ROLLING_FILE_LOGGER)
	{
		cout<<"[INFO] Try to new FixedSizeRollingFileLogger"<<endl;
		va_list args;
		va_start(args, buffersize);
		char* logfile = va_arg(args, char*);
		int fixedsize = va_arg(args, int);
		va_end(args);

		Logger* logger = new FixedSizeRollingFileLogger(logfile, buffersize, fixedsize);
		if(!logger->init())
		{
			delete logger;
			cout<<"[FATAL] Init FixedSizeRollingFileLogger failed"<<endl;
			return NULL;
		}

		return logger;
	}
	else if(type == DAILY_ROLLING_FILE_LOGGER)
		{
			cout<<"[INFO] Try to new DailyRollingFileLogger"<<endl;
			va_list args;
			va_start(args, buffersize);
			char* logfile = va_arg(args, char*);
			va_end(args);

			Logger* logger = new DailyRollingFileLogger(logfile, buffersize);
			if(!logger->init())
			{
				delete logger;
				cout<<"[FATAL] Init DailyRollingFileLogger failed"<<endl;
				return NULL;
			}

			return logger;
		}
	else if(type == CONSOLE_LOGGER)
	{
		cout<<"[INFO] Try to new ConsoleLogger"<<endl;
		Logger* logger = new ConsoleLogger(buffersize);
		if(!logger->init())
		{
			delete logger;
			cout<<"[FATAL] Init ConsoleLogger failed"<<endl;
			return NULL;
		}

		return logger;
	}
	return NULL;
}


